<template>


    <a-modal :title="title" :width="600" :destroyOnClose="true" :maskClosable="false" :visible="visible"
             @cancel="handleCancel">


        <a-form-model ref="ruleForm" :model="form" :rules="rules">

            <a-form-model-item ref="oldPlateNo" label="当前车牌号" prop="oldPlateNo">
                <a-input v-model="form.oldPlateNo" :disabled="true" @blur="() => {$refs.oldPlateNo.onFieldBlur();}"/>
            </a-form-model-item>

            <a-form-model-item ref="oldPlateNo" label="待付金额">
                <a-input v-model="record.arrearsFee/100 +' 元'" :disabled="true"/>
            </a-form-model-item>

            <a-form-model-item ref="fee" label="减免金额(单位:元)" prop="fee">
                <a-input v-model.number="form.fee" @blur="() => {$refs.fee.onFieldBlur();}"/>
            </a-form-model-item>


        </a-form-model>


        <!-- Modal 页脚 -->
        <template slot="footer">
            <a-button @click="handleCancel">关闭</a-button>
            <a-button type="primary" :loading="loading" @click="onSubmit">保存</a-button>
        </template>

    </a-modal>

</template>
<script>
    import {modifyFee} from '../public/http.js';

    export default {

        props: ["record"],
        data() {
            return {
                loading: false, //按钮Loading
                title: "减免费用",
                visible: true, //modal 显示状态


                form: {
                    oldPlateNo: null,
                    fee: null,
                },

                rules: {
                    oldPlateNo: [
                        {required: true, message: '当前车牌号不能为空', trigger: 'blur'},

                    ],
                    fee: [
                        {
                            required: true,
                            message: '调整金额不能为空',
                            trigger: 'blur'
                        },
                        {
                            type: "number",
                            message: '调整金额必须为数字',
                            trigger: 'blur'
                        },

                    ],

                },


            };
        },
        methods: {


            //关闭弹窗
            handleCancel() {
                this.visible = false;
                this.$emit('closeModal', {
                    render: false
                });
            },


            //完成监听
            onSubmit(e) {


                this.$refs.ruleForm.validate(valid => {
                    if (valid) {

                        if (this.form.fee * 100 > this.record.arrearsFee) {
                            this.$notification.error({
                                message: '禁止操作',
                                description: '减免费用不能大于待付金额'
                            })
                            return false;
                        }

                        let data = {id: this.record.id, fee: this.form.fee}


                        modifyFee(data).then(res => {
                            this.$exMod.ajaxMessage(res);
                            if (res.code === 0) {
                                //关闭弹窗
                                this.visible = false;
                                this.$emit('closeModal', {render: true});
                            }
                        });


                    }
                })


            },


        },


        created() {
            this.form.oldPlateNo = this.record.plateNo
        }
    };
</script>
